package edu.hrbu.trade01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hrbu.trade01.api.MenuApi;
import edu.hrbu.trade01.entity.Menu;
import edu.hrbu.trade01.entity.RoleMenu;
import edu.hrbu.trade01.entity.UserRole;
import edu.hrbu.trade01.mapper.MenuMapper;
import edu.hrbu.trade01.service.MenuService;
import edu.hrbu.trade01.service.RoleMenuService;
import edu.hrbu.trade01.service.UserRoleService;
import edu.hrbu.trade01.util.MenuUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 徐登宇
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleMenuService roleMenuService;

    // 获取菜单树
    @Override
    public MenuApi getMenuTreeByUserId(Long userId) {

        List<Menu> ownMenuList = getOwnMenuListByUserId(userId).get("ownMenuList");

        ownMenuList = MenuUtil.buildTree(ownMenuList);
        log.info("@@@ "+ ownMenuList);

        MenuApi menuApi = new MenuApi();
        menuApi.setMenuInfo(ownMenuList);
        return menuApi;
    }

    // 通过userid获取自己拥有的菜单
    @Override
    public Map<String,List<Menu>> getOwnMenuListByUserId(Long userId) {
        // 【userrole表】通过userid查询roleidlist
        LambdaQueryWrapper<UserRole> wrapperUserRole = new LambdaQueryWrapper<>();
        wrapperUserRole.eq(UserRole::getUserId, userId);
        List<Long> ownRoleIdList = userRoleService.list(wrapperUserRole).stream().map(UserRole::getRoleId).collect(Collectors.toList());

        ArrayList<Long> ownMenuIdList = new ArrayList<>();
        // 【rolemenu表】通过roleid查询menuidlist
        List<RoleMenu> roleMenuList = roleMenuService.list();
        for (RoleMenu rm : roleMenuList) {
            if (ownRoleIdList.contains(rm.getRoleId()) && rm.getIsDeleted()==0) {
                ownMenuIdList.add(rm.getMenuId());
            }
        }

        log.info("@ownMenuIdList--"+ ownMenuIdList);

        ArrayList<Menu> ownMenuList = new ArrayList<>();
        // 【menu表】获取menulist
        List<Menu> menuList = list();
        // 【menu表】通过menuidlist与menulist的id做匹配，匹配上的，就是对应的菜单
        for (Menu m:menuList) {
            if (ownMenuIdList.contains(m.getId()) && m.getIsDeleted()==0) {
                ownMenuList.add(m);
            }
        }

        HashMap<String, List<Menu>> map = new HashMap<>();
        map.put("ownMenuList", ownMenuList);

        return map;
    }

    // 条件分页查询
    @Override
    public Page<Menu> getPageWithCondition(int page, int limit, String name) {
        Page<Menu> pageParam = new Page<>(page, limit);
        // 获取菜单列表
        LambdaQueryWrapper<Menu> wrapperMenu = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(name)){
            wrapperMenu.like(Menu::getTitle, name);
        }
        // 所有的菜单
        return page(pageParam, wrapperMenu);
    }

    // 通过roleid获取自己拥有的菜单以及所有的菜单
    @Override
    public Map<String, List<Menu>> getMenuMapByRoleId(Long roleId) {
        List<Menu> allMenuList = list();

        LambdaQueryWrapper<RoleMenu> wrapperRoleMenu = new LambdaQueryWrapper<>();
        wrapperRoleMenu.eq(RoleMenu::getRoleId, roleId);
        List<Long> ownMenuIdList = roleMenuService.list(wrapperRoleMenu).stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        ArrayList<Menu> ownMenuList = new ArrayList<>();
        for (Menu m:allMenuList) {
            if (ownMenuIdList.contains(m.getId())) ownMenuList.add(m);
        }

//        allMenuList = MenuUtil.buildTree(allMenuList,1);
        HashMap<String, List<Menu>> map = new HashMap<>();
        map.put("allMenuList", allMenuList);
        map.put("ownMenuList", ownMenuList);
        return map;
    }

}
